/*
 * @lc app=leetcode.cn id=202 lang=cpp
 *
 * [202] 快乐数
 */
class Solution
{
public:
    bool isHappy(int n)
    {
        unordered_set<int> st;
        while (n != 1)
        {
            int sum = 0;
            while (n)
            {
                sum += (n % 10) * (n % 10);
                n /= 10;
            }
            n = sum;
            if (st.count(n))
            {
                break;
            }
            st.insert(n);
        }
        return n == 1;
    }
};
